﻿/*
 * @author: wizardc
 */

using System;
using UnityEngine;
using UnityEngine.UI;

namespace Dou.UI
{
    /// <summary>
    /// 贴图图片
    /// </summary>
    [ExecuteInEditMode]
    public class DRawImage : RawImage
    {
        private string _textureName = string.Empty;
        [SerializeField]
        private bool _useGray = false;
        [SerializeField]
        private bool _useNativeSize = false;

        /// <summary>
        /// 贴图资源名称
        /// </summary>
        public string textureName
        {
            set => SetTextureName(value);
            get => _textureName;
        }

        /// <summary>
        /// 是否置灰
        /// </summary>
        public bool useGray
        {
            set
            {
                if (_useGray != value)
                {
                    _useGray = value;
                    SetGray(_useGray);
                }
            }
            get => _useGray;
        }

        /// <summary>
        /// 是否默认使用原始尺寸
        /// </summary>
        public bool useNativeSize
        {
            set
            {
                if (_useNativeSize != value)
                {
                    _useNativeSize = value;
                    if (_useNativeSize)
                    {
                        SetNativeSize();
                    }
                }
            }
            get => _useNativeSize;
        }

        protected override void Awake()
        {
            base.Awake();
            if (_useGray)
            {
                SetGray(true);
            }
        }

        public void SetTextureName(string textureName, Action callback = null)
        {
            if (_textureName.Equals(textureName))
            {
                return;
            }
            if (!string.IsNullOrEmpty(_textureName))
            {
                texture = UIAssetUtil.transparentTex;
                if (!UIAssetUtil.ReleaseObject(_textureName))
                {
                    Debug.LogWarning($"资源{_textureName}回收失败！");
                }
            }
            _textureName = textureName;
            if (!string.IsNullOrEmpty(_textureName))
            {
                UIAssetUtil.LoadObject(_textureName, (p, obj) =>
                {
                    // 回调之前又修改了精灵名称
                    if (p != _textureName)
                    {
                        return;
                    }
                    if (gameObject != null && obj != null)
                    {
                        texture = obj as Texture;
                        if (_useNativeSize)
                        {
                            SetNativeSize();
                        }
                        callback?.Invoke();
                    }
                });
            }
        }

        public void SetColor(float r, float g, float b, float a = 1)
        {
            color = new Color(r, g, b, a);
        }

        public void SetColor32(int r, int g, int b, int a = 255)
        {
            color = new Color(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
        }

        private void SetGray(bool isGray)
        {
            material = isGray ? UIAssetUtil.grayMat : null;
        }

        protected override void OnDisable()
        {
            base.OnDisable();
            if (!string.IsNullOrEmpty(_textureName))
            {
                UIAssetUtil.ReleaseObject(_textureName);
                _textureName = null;
            }
        }
    }
}
